<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Category extends BackendController
{
	function __construct()
	{
		parent::__construct();

		$this->lang->load('category');
		$this->load->model('category_model');
	}
	public function index()
	{
		$keyword = $this->filter->get('keyword');
		if($keyword)
		{
			$where_like = $this->db->escape('%'.$this->db->escape_like_str($keyword).'%');
			$this->category_model->list_where = 'title LIKE '.$where_like;
		}

		$this->category_model->list_ordering = $this->filter->get('ordering', 'id');
		$this->category_model->list_direction = $this->filter->get('direction', 'DESC');

		$items = array();
		$rows = $this->category_model->getRoot();
		foreach($rows as $row)
		{
			$row->parent = null;
			$items[] = $row;
			if($row->child)
			{
				foreach($row->child as $child_id)
				{
					$child = $this->category_model->getItem($child_id);
					$child->title = '&ndash;&ndash; '.$child->title;
					$child->parent = null;
					$child->parent = $this->category_model->getItem($child->parent_id);
					$items[] = $child;
				}
			}
		}

		$link = site_url('admin/category/index');
		$data = array(
			'link'=>$link,
			'items'=>$items,
			'filter'=>$this->filter->getdata(),
			'ordering'=>$this->category_model->list_ordering,
			'direction'=>$this->category_model->list_direction,
		);

		$this->filter->store();
		if($this->input->get('ajax'))
		{
			//$this->output->set_template('ajax');
			$temp = 'items_list';
		}
		else
		{
			$this->output->set_template('backend');
			$this->output->set_title(lang('category_manager'));
			$temp = 'items';
		}
		$this->load->view('backend/category/'.$temp, $data);
	}
	public function form($id=0)
	{
		$id = intval($id);
		if($id)
		{
			$item = $this->category_model->getItem($id);
			if(!$item)
			{
				$this->messages->add(lang('not_found'), 'e');
				redirect(site_url('admin/category/index'));
			}
			$this->output->set_title(lang('category_edit'));
		}
		else
		{
			$item = $this->category_model->blankItem();
			$item->published = 1;
			$this->output->set_title(lang('category_add'));
		}

		$this->load->library('form_validation');
		$this->form_validation->set_rules('parent_id', 'lang:parent', 'is_natural');
		$this->form_validation->set_rules('title', 'lang:title', 'trim|required|min_length[3]|xss_clean');
		$this->form_validation->set_rules('description');
		$this->form_validation->set_rules('tag');

		if ($this->form_validation->run() == TRUE)
		{
			$data = $this->input->post();
			$data['alias'] = urlTitle($data['title']);
			$data['path'] = $data['alias'];
			if($data['parent_id'])
			{
				$parent = $this->category_model->getItem($data['parent_id']);
				$data['path'] = $parent->path.'/'.$data['path'];
			}
			if($data['tag'])
			{
				$tags = preg_split('/\s*,\s*/',trim($data['tag']),-1,PREG_SPLIT_NO_EMPTY);
				$data['tag'] = implode(',', $tags);
			}

			$msg = $this->category_model->save($data);
			if($id)
				$this->messages->add(lang('update_successfully'));
			else
				$this->messages->add(lang('add_successfully'));

			redirect(site_url('admin/category/index'));
		}
		else
		{
			$this->messages->add(validation_errors(), 'e');
		}

		$this->category_model->list_where = 'id <> '.$id.' AND parent_id = 0';
		$rows = $this->category_model->getList();
		$parent = array(''=>lang('root'));
		foreach($rows as $row)
		{
			$parent[$row->id] = $row->title;
		}
		$data = array(
			'item'=>$item,
			'parent'=>$parent,
		);

		$this->output->set_template('backend');
		$this->load->view('backend/category/form', $data);
	}
	public function toggle($field='', $id=0, $val=1)
	{
		$id = intval($id);
		if(in_array($field, array('published')))
		{
			$data['id'] = $id;
			$data[$field] = intval($val);
			$this->category_model->update($data);
			$this->messages->add(lang('change_successfully'));
		}
		$ajax = '';
		if($this->input->get('ajax'))
		{
			$ajax = '?ajax=1';
		}
		redirect(site_url('admin/category/index').$ajax);
	}
	public function delete($id=null)
	{
		$id = intval($id);
		$posts_total = $this->db->from('posts')->where('category_id', $id)->count_all_results();
		if($posts_total==0 && $this->category_model->delete($id))
		{
			$this->messages->add(lang('delete_successfully'));
		}
		else
		{
			$this->messages->add(lang('delete_unsuccessfully_posts_exists'));
		}
		$ajax = '';
		if($this->input->get('ajax'))
		{
			$ajax = '?ajax=1';
		}
		redirect(site_url('admin/category/index').$ajax);
	}

}
